export class DataManager {
    private static _instance: DataManager;
    public static get instance(): DataManager {
        if (!this._instance) {
            this._instance = new DataManager();
        }
        return this._instance;
    }

    private constructor() {}

    private dataStore: Map<string, any> = new Map();

    /**
     * 设置内存数据
     */
    public set(key: string, value: any): void {
        this.dataStore.set(key, value);
    }

    /**
     * 获取内存数据
     */
    public get<T>(key: string): T | undefined {
        return this.dataStore.get(key);
    }

    /**
     * 移除内存数据
     */
    public remove(key: string): void {
        this.dataStore.delete(key);
    }

    /**
     * 清空内存数据
     */
    public clear(): void {
        this.dataStore.clear();
    }

    /**
     * 保存数据到本地 localStorage
     * @example
     * DataManager.instance.saveLocal('user', {name: 'Tom'});
     */
    public saveLocal(key: string, value: any): void {
        localStorage.setItem(key, JSON.stringify(value));
    }

    /**
     * 从本地 localStorage 读取数据
     * @example
     * const user = DataManager.instance.loadLocal('user');
     */
    public loadLocal<T>(key: string): T | null {
        const str = localStorage.getItem(key);
        if (!str) return null;
        try {
            return JSON.parse(str) as T;
        } catch {
            return null;
        }
    }

    /**
     * 移除本地 localStorage 数据
     * @example
     * DataManager.instance.removeLocal('user');
     */
    public removeLocal(key: string): void {
        localStorage.removeItem(key);
    }

    /**
     * 保存数据到线上服务器（需配合 NetworkManager 使用）
     * @example
     * await DataManager.instance.saveRemote('/api/save', {score: 100});
     */
    public async saveRemote(url: string, data: any): Promise<any> {
        // 需配合 NetworkManager
        // return await NetworkManager.instance.post(url, data);
        // 这里仅做示例
        return Promise.resolve({ code: 0, msg: '模拟线上存储成功' });
    }

    /**
     * 从线上服务器读取数据（需配合 NetworkManager 使用）
     * @example
     * const data = await DataManager.instance.loadRemote('/api/load', {userId: 1});
     */
    public async loadRemote(url: string, params?: any): Promise<any> {
        // 需配合 NetworkManager
        // return await NetworkManager.instance.get(url, params);
        // 这里仅做示例
        return Promise.resolve({ code: 0, data: null, msg: '模拟线上读取成功' });
    }
} 